<?php

namespace App;

use App\Http\Services\FileManager;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;


/**
 * App\OrderContract
 *
 * @property int $id
 * @property int $order_id 订单id
 * @property int $contract_id 合同id
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Files $files
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderContract newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderContract newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\OrderContract onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderContract query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderContract whereContractId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderContract whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderContract whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderContract whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderContract whereOrderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\OrderContract whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\OrderContract withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\OrderContract withoutTrashed()
 * @mixin \Eloquent
 */
class OrderContract extends Model
{
    use SoftDeletes;

    public static function getOrderContractList($orderId = 0){
        if(empty($orderId))
            return [];

        $data = [];
        /** @var OrderContract $orderContract */
        $orderContractList = self::whereOrderId($orderId)->get();
        if(count($orderContractList) > 0){
            foreach ($orderContractList as $orderContract) {
                $contractUrl = FileManager::getOrderContractUrl($orderContract->contract_id);
                $contract = Contract::whereId($orderContract->contract_id)->first();

                $value = ['id' => $orderContract->contract_id,'name' => !empty($contract) ? $contract->name : '','url' => $contractUrl];
                array_push($data,$value);
            }
        }

        return $data;
    }

    public function files(){
        return $this->belongsTo('App\Files','file_id');
    }
}
